package cn.hotel.servlet;

import cn.hotel.pojo.Member;
import cn.hotel.service.MemberService;
import cn.hotel.service.MemberServiceImpl;
import cn.hotel.utlis.MapUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet("/memberregister")
public class MemberRegisterServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行处理GET请求的方法.................");
        doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行处理POST请求的方法.................");
        PrintWriter out = resp.getWriter();
        String appPath = req.getContextPath();
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Member member = new Member();
        member.setAccount(account);
        member.setPassword(password);
        MemberService memberService = new MemberServiceImpl();
        Map<String,Object> resultMap = memberService.register(member);
        String flag = MapUtil.getString(resultMap,"flag");
        if ("1".equals(flag)){
            out.print("<script>alert('注册成功');location.href='"+appPath+"/memberjsp/login.jsp'</script>");
        }else {
            out.print("<script>alert('注册失败');location.href='"+appPath+"/memberjsp/register.jsp'</script>");
        }




    }


}
